﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class KC_BZX_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindFirst();
        }

        private void BindFirst()
        {
            BindCK(ddlCKID, "请选择");
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (string.IsNullOrEmpty(this.hidID.Value))
            {
                this.lbResult.Text = "请选择包装箱";
                return;
            }
            else
            {
                //using (KCBLL kBLL = new KCBLL())
                //{
                //    if (kBLL.IsExist(t => t.WPID == new Guid(this.hidID.Value)))
                //    {
                //        this.lbResult.Text = "此物料已存在，不能重复建仓";
                //        return;
                //    }
                //}
            }
            string resultMessage = "";
            try
            {
                string temp = "ctl00$ContentPlaceHolder1$";
                string[] CKids = Request.Form[temp + "ddlCKID"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] quStr = Request.Form[temp + "txtQU"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] hangStr = Request.Form[temp + "txtHANG"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] lieStr = Request.Form[temp + "txtLIE"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] cengStr = Request.Form[temp + "txtCENG"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] geStr = Request.Form[temp + "txtGE"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] slStr = Request.Form[temp + "txtSL"].Split(new string[] { "," }, StringSplitOptions.None);
                using (ZL_CWBLL cBLL = new ZL_CWBLL())
                {
                    bool IsEmpty = true; //仓位空置
                    List<ZL_CW> cwList = new List<ZL_CW>();
                    for (int i = 0; i < CKids.Length; i++)
                    {
                        IsEmpty = cBLL.IsEmpty(CKids[i], quStr[i], hangStr[i], lieStr[i], cengStr[i], geStr[i]);
                        if (!IsEmpty)
                        {
                            resultMessage = string.Format("仓位“{0}-{1}-{2}-{3}-{4}”已放置物品，不能在此建仓", quStr[i], hangStr[i], lieStr[i], cengStr[i], geStr[i]);
                            break;
                        }
                        ZL_CW cw = new ZL_CW();
                        cw.ID = Guid.NewGuid();
                        cw.BH = "";
                        cw.CKID = new Guid(CKids[i]);
                        cw.QU = quStr[i];
                        cw.HANG = hangStr[i];
                        cw.LIE = lieStr[i];
                        cw.CENG = cengStr[i];
                        cw.GE = geStr[i];
                        //库存信息
                        KC entity = new KC();
                        entity.ID = Guid.NewGuid();
                        entity.WPID = Guid.Parse(hidID.Value);
                        entity.WPLX = "03";
                        entity.CKID = cw.CKID;
                        entity.SL = string.IsNullOrEmpty(slStr[i]) ? 0 : Convert.ToDouble(slStr[i]);
                        entity.PJDJ = entity.ZXDJ;
                        cw.KC.Add(entity);

                        cwList.Add(cw);
                    }
                    if (IsEmpty)
                    {
                        if (cBLL.Inserts(cwList))
                        {
                            btnSave.Visible = false;
                            resultMessage = "保存成功";
                        }
                        else
                            resultMessage = "保存失败";
                    }
                }
            }
            catch
            {
                resultMessage = "保存失败";
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }
    }
}